package com.niuxiaofa.newlaw.Network;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.luck.picture.lib.tools.ToastUtils;
import com.niuxiaofa.newlaw.Activity.LoginActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Tools.Constant;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Gxjkz on 2017/12/20.
 */

public class ServiceManager {
    Gson gson = new Gson();
    private static ServiceManager mServiceManage, mServiceManage2;
    private Retrofit retrofit;
    int DEFAULT_OUT_TIME = 10;
    BaseBean baseBean;
    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request()
                            .newBuilder()
//                            .addHeader("Authorization", Constant.TOKEN)
                            .addHeader("Authorization", Constant.TOKEN)
                            .build();
                    Response response = chain.proceed(request);


                    if (response.code() != 200) {
                        return response;
                    }

                    ResponseBody responseBody = response.peekBody(1024 * 1024);
                    byte[] respBytes = responseBody.bytes();
                    String respString = new String(respBytes);
                    Log.e("response",respString);


                    try {
                        baseBean = gson.fromJson(respString, BaseBean.class);

                        switch (baseBean.getCode()) {
                            case 401://未登录
                                Constant.ISLOGIN = false;
                                ToActivityUtil.toNextActivity(LoginActivity.class);
                                break;

                        }
                    } catch (Exception e) {
                        Log.e("erroRespString", respString);
                    }
                    return response;
                }
            })
            .build();


    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                case 0:
                    //   ToActivityUtil.toNextActivity(LoginActivity.class);
                    //   Toast.makeText(UIUtils.getActivity(),baseBean.getMsg(),Toast.LENGTH_LONG).show();
                    break;
                case 1:

                    break;

            }

        }
    };

    public static ServiceManager getInstance() {
        if (mServiceManage == null) {
            mServiceManage = new ServiceManager(Api.BASE_API);
            mServiceManage.creat(ApiService.class);
        }
        return mServiceManage;
    }


    private ServiceManager(String baseUrl) {
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        // RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),data);
    }


//    private ServiceManager(String baseUrl, int timeout) {
//        OkHttpClient clientTime = new OkHttpClient.Builder()
//                .connectTimeout(2, TimeUnit.SECONDS)
//                .readTimeout(timeout, TimeUnit.SECONDS)
//                .writeTimeout(2, TimeUnit.SECONDS)
//                .build();
//        retrofit=new Retrofit.Builder()
//                .client(clientTime)
//                .baseUrl(baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .build();
//        // RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),data);
//
//    }

    public <T> T creat(Class<T> service) {
        return retrofit.create(service);
    }


}
